//
//  MQOrderManagerViewController.m
//  meiquan
//
//  Created by hejingjin on 2017/10/23.
//  Copyright © 2017年 mq. All rights reserved.
//

#import "MQOrderManagerViewController.h"
#import "MQMyOrderCell.h"
#import "MQOrderDetailViewController.h"
#import "MQadviews.h"
#import "MQAdmodels.h"
#import "MQBaseUserInfoModel.h"
#import "MQAdDetailViewController.h"
#import "MQOrderListmodel.h"
#import "MQPaymentviewViewController.h"
@interface MQOrderManagerViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong)UITableView *ordertableview;

@property (nonatomic,strong)NSMutableArray *dataSource;

@property (nonatomic,strong)NSArray *titlearray;

@property (nonatomic,assign)NSInteger currentselectindex;

@property (nonatomic,strong)NSMutableArray *buttonarray;

@property (nonatomic,strong)NSMutableArray *bottomviewarray;

@property (nonatomic,strong)UIView *topview;

@property (nonatomic,assign)NSInteger allpage;

@property(nonatomic,assign)NSInteger waitpaypage;
@property(nonatomic,assign)NSInteger waitphippage;
@property(nonatomic,assign)NSInteger waitconfirmpage;
@property(nonatomic,assign)NSInteger confimpage;

// @[@"all",@"wait_pay",@"wait_ship",@"wait_confirm",@"confim"]

@property(nonatomic,strong)MQadviews *adviews;

@property(nonatomic,strong)MQAdmodels *admodels;


@property(nonatomic,strong)NSMutableArray *waitfahuodatasource;
@property(nonatomic,strong)NSMutableArray *flishdatasource;
@property(nonatomic,strong)NSMutableArray *waitpayarray;
@property(nonatomic,strong)NSMutableArray *waitmakesuredatasource;


@end

@implementation MQOrderManagerViewController


-(void)getaddata{
    
    NSArray *array = @[@"page1",@"@page2",@"page3"];
    int x = arc4random() % 3;
    
    MQBaseUserInfoModel *userinfo = [MQBaseUserInfoModel shareInstant];
    [userinfo loadUserinfoFromSadeBox];
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    
    [param setValue:userinfo.userId forKey:@"userId"];
    [param setValue:array[x] forKey:@"code"];
    
    [MQRequestTools POSTWithMethod:adv_index params:param success:^(id json) {
        
        NSDictionary *dic = [json valueForKey:@"data"];
        _admodels = [MQAdmodels mj_objectWithKeyValues:dic];
        if (_admodels.advId) {
            [_adviews.adimageviews sd_setImageWithURL:[NSURL URLWithString:_admodels.pic] placeholderImage:[UIImage imageNamed:@""]];
        }else{
            _adviews.bst_height = 0;
            _ordertableview.bst_y = MAX_Y(_topview.frame);
            _ordertableview.bst_height =BSTScreenH -MAX_Y(_topview.frame);
        }
        
        
        
        UITapGestureRecognizer *tapGesturRecognizer=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
        [_adviews addGestureRecognizer:tapGesturRecognizer];
        
    } failure:^(NSError *error) {
        
    }];
    
    
}

-(void)tapAction:(UITapGestureRecognizer *)obj{
    NSLog(@"%@",_admodels.url);
    MQAdDetailViewController *advc = [[MQAdDetailViewController alloc] init];
    advc.url = _admodels.url;
     advc.type = _admodels.type;
    [self.navigationController pushViewController:advc animated:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    

    _dataSource = [NSMutableArray array];
    
    
    _waitfahuodatasource = [NSMutableArray array];
    _flishdatasource = [NSMutableArray array];
    _waitpayarray = [NSMutableArray array];
    _waitmakesuredatasource = [NSMutableArray array];
    
    _buttonarray = [NSMutableArray array];
    
    _bottomviewarray = [NSMutableArray array];
    
    
    _currentselectindex = 0;
    
    _allpage = _waitpaypage =_waitphippage = _waitconfirmpage = _confimpage = 0;
    
    _titlearray = @[@"全部",@"待支付",@"待发货",@"待确认",@"完成"];
    
    [self setUpMainInterface];
    
    
    [self getaddata];
    self.title = @"我的订单";
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 167.f;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    switch (_currentselectindex) {
        case 0:
            return _dataSource.count;
//            return 10;
            break;
        case 1:
            return _waitpayarray.count;
            break;
        case 2:
            return _waitfahuodatasource.count;
            break;
        case 3:
            return _waitmakesuredatasource.count;
            break;
        case 4:
            return _flishdatasource.count;
            break;
            
        default:
            return 0;
            break;
    }
    
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
       MQMyOrderCell *cell = [MQMyOrderCell CellWithTableview:tableView];
    MQOrderListmodel *model;
    
    
    
    switch (_currentselectindex) {
        case 0:
        {
            model = _dataSource[indexPath.row];
            
        }
            
            break;
        case 1:
        {
            model = _waitpayarray[indexPath.row];
        }
            break;
        case 2:
        {
            model = _waitfahuodatasource[indexPath.row];
        }
            break;
        case 3:
        {
             model = _waitmakesuredatasource[indexPath.row];
        }
            break;
        case 4:
        {
             model = _flishdatasource[indexPath.row];

        }
            break;
            
        default:
            
            break;
    }
    cell.model = model;
    [cell.productimageview sd_setImageWithURL:[NSURL URLWithString:model.productPic]];
    cell.productnameLabel.text = model.productName;
    cell.orderCodeLabel.text = [NSString stringWithFormat:@"订单编号  %@",model.ID];
    cell.buynameLabel.text = [NSString stringWithFormat:@"购买人:  %@",model.buyer];
    cell.totalprice.text = [NSString stringWithFormat:@"¥ %@",model.payTotal];
    cell.unitpriceLabel.text =[NSString stringWithFormat:@"¥ %@",model.productPrice];
    cell.productcountlabel.text = [NSString stringWithFormat:@"x%@",model.productCount];
    [cell.firstbutton addTarget:self action:@selector(firstbuttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [cell.lastbutton addTarget:self action:@selector(lastbuttonPressed:) forControlEvents:UIControlEventTouchUpInside];
//     NSArray *paramarray = @[@"all",@"wait_pay",@"wait_ship",@"wait_confirm",@"confim"];
    
    if ([model.status isEqualToString:@"wait_pay"]) {
        cell.firstbutton.hidden = YES;
        cell.lastbutton.hidden = NO;
        cell.paybuttonW.constant = 90;
        [cell.lastbutton setTitle:@"确认付款" forState:UIControlStateNormal];
    }else if ([model.status isEqualToString:@"wait_ship"]){
        cell.firstbutton.hidden = YES;
        cell.lastbutton.hidden = YES;
    }else if([model.status isEqualToString:@"wait_confirm"]){
        cell.firstbutton.hidden = NO;
        cell.lastbutton.hidden = NO;
          cell.paybuttonW.constant = 90;
        [cell.lastbutton setTitle:@"确认收货" forState:UIControlStateNormal];
    }else{
        cell.paybuttonW.constant = 0;
        cell.firstbutton.hidden = NO;
        cell.lastbutton.hidden = YES;
    }
    
    
    
    return cell;
}

-(void)firstbuttonPressed:(UIButton *)sender{
    
    //申请售后 打电话
}

-(void)lastbuttonPressed:(UIButton *)sender{
    MQMyOrderCell *cell = (MQMyOrderCell *)sender.superview.superview;
    MQOrderListmodel *model = cell.model;
    if ([model.status isEqualToString:@"wait_pay"]) {
        MQPaymentviewViewController *paymentvc = [[MQPaymentviewViewController alloc] init];
        paymentvc.model = model;
        [self.navigationController pushViewController:paymentvc animated:YES];
    }else{
        
    }
    //    NSIndexPath *indexpath = [_ordertableview indexPathForCell:cell];
 
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MQOrderListmodel *model;
    switch (_currentselectindex) {
        case 0:
        {
            model = _dataSource[indexPath.row];
        }
            
            break;
        case 1:
        {
            model = _waitpayarray[indexPath.row];
        }
            break;
        case 2:
        {
            model = _waitfahuodatasource[indexPath.row];
        }
            break;
        case 3:
        {
            model = _waitmakesuredatasource[indexPath.row];
        }
            break;
        case 4:
        {
            model = _flishdatasource[indexPath.row];
            
        }
        default:
     
            break;
    }
    

    
    MQOrderDetailViewController *orderdetailvc = [[MQOrderDetailViewController alloc] init];
    orderdetailvc.ordermodel = model;
    [self.navigationController pushViewController:orderdetailvc animated:YES];
}

-(void)setUpMainInterface{
    
    self.view.backgroundColor = BSTColorFromRGB(0xefefef);
    
    
    UIView *topview = [[UIView alloc] initWithFrame:CGRectMake(0, NAVHEIGHT, BSTScreenW, 45)];
    topview.backgroundColor = [UIColor whiteColor];
    for (int i = 0; i <5; i ++ ) {
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(i * BSTScreenW/5, 0, BSTScreenW/5, 43)];
        [button setTitle:_titlearray[i] forState:UIControlStateNormal];
        
        [button setTitleColor:BSTColorFromRGB(0x333333) forState:UIControlStateNormal];
        [button setTitleColor:BSTColorFromRGB(0xff8e01) forState:UIControlStateSelected];
        button.titleLabel.font = [UIFont systemFontOfSize:14];
        [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
        
        UIView *bottomview = [[UIView alloc] initWithFrame:CGRectMake(i * BSTScreenW/5 +  + BSTScreenW/5/3 *1/2, 43, BSTScreenW/5 * 2 / 3, 2)];
        bottomview.hidden = YES;
        bottomview.backgroundColor = BSTColorFromRGB(0xff8e01);
        button.tag = 10000+ i;
        [_buttonarray addObject:button];
        [_bottomviewarray addObject:bottomview];
        if (i == 0) {
            button.selected = YES;
            bottomview.hidden = NO;
        }
        [topview addSubview:button];
        [topview addSubview:bottomview];
    }
    _topview = topview;
    [self.view addSubview:topview];
    
    _adviews = [MQadviews loadadviews];
    _adviews.bst_x = 0;
    _adviews.bst_width = BSTScreenW;
    _adviews.bst_height = 70 *Persion;
    _adviews.bst_y = MAX_Y(_topview.frame);
    [self.view addSubview:_adviews];
    
    [self.view addSubview:self.ordertableview];
    
    
}

-(void)buttonPressed:(UIButton *)sender{
    
    
    NSInteger index = sender.tag  - 10000;
    
    if (index == _currentselectindex) {
        return;
    }else{
    
        _currentselectindex = index;
        [_ordertableview reloadData];
        [_ordertableview.mj_header beginRefreshing];
        for (int i = 0; i <5; i ++) {
            UIButton *button = _buttonarray[i];
            UIView *bottomview = _bottomviewarray[i];
            
            if (_currentselectindex == i) {
                button.selected =YES;
                bottomview.hidden = NO;
            }else{
                button.selected =NO;
                bottomview.hidden = YES;
            }
        }
        
    }
    
    
}

-(UITableView *)ordertableview{
    if (!_ordertableview) {
        
        
        _ordertableview = [[UITableView alloc] initWithFrame:CGRectMake(0, MAX_Y(_topview.frame) + 10 * Persion + 70 * Persion, BSTScreenW, BSTScreenH - 80 * Persion - MAX_Y(_topview.frame)) style:UITableViewStylePlain];
        _ordertableview.delegate = self;
        _ordertableview.dataSource = self;
        _ordertableview.backgroundColor = BSTColorFromRGB(0xf5f5f5);
        _ordertableview.showsVerticalScrollIndicator = NO;
        UIView *footerview = [UIView new];
        _ordertableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        footerview.backgroundColor = BSTColorFromRGB(0xf5f5f5);
        _ordertableview.tableFooterView = footerview;
        _ordertableview.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [self getorderData];
        }];
        
        [_ordertableview.mj_header beginRefreshing];
        _ordertableview.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
            [self getmoredata];
        }];
        
    }
    return _ordertableview;
}


-(void)getorderData{
    
    MQBaseUserInfoModel *userinfo = [MQBaseUserInfoModel shareInstant];
    [userinfo loadUserinfoFromSadeBox];
    
    switch (_currentselectindex) {
        case 0:
            _allpage = 0;
            break;
            
        case 1:
            _waitpaypage = 0;
            break;
        case 2:
            _waitphippage = 0;
            break;
        case 3:
            _waitconfirmpage = 0;
            break;
        case 4:
            _confimpage = 0;
            break;
        default:
            break;
    }

    
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    
    NSArray *paramarray = @[@"all",@"wait_pay",@"wait_ship",@"wait_confirm",@"confim"];
    [param setValue:userinfo.userId forKey:@"userId"];
    if (_currentselectindex !=0) {
          [param setValue:paramarray[_currentselectindex] forKey:@"status"];
    }
    [param setValue:@"0" forKey:@"page"];
    [param setValue:@"10" forKey:@"limit"];
    
    [MQRequestTools GETWithMethod:orderInfo_list params:param success:^(id json) {
        
        NSArray *dataarray = [json valueForKey:@"data"];
        switch (_currentselectindex) {
            case 0:
            {
                [_dataSource removeAllObjects];
                for (NSDictionary *dic in dataarray) {
                    MQOrderListmodel *model = [MQOrderListmodel mj_objectWithKeyValues:dic];
                    [_dataSource addObject:model];
                }
                _allpage ++;
                
            }
                break;
            case 1:
            {
                [_waitpayarray removeAllObjects];
                
                for (NSDictionary *dic in dataarray) {
                    MQOrderListmodel *model = [MQOrderListmodel mj_objectWithKeyValues:dic];
                    [_waitpayarray addObject:model];
                }
                _waitpaypage ++;
                
            }
                break;
            case 2:
            {
                [_waitfahuodatasource removeAllObjects];
                
                for (NSDictionary *dic in dataarray) {
                    MQOrderListmodel *model = [MQOrderListmodel mj_objectWithKeyValues:dic];
                    [_waitfahuodatasource addObject:model];
                }
                _waitphippage ++;
                
            }
                break;
            case 3:
            {
                [_waitmakesuredatasource removeAllObjects];
                
                for (NSDictionary *dic in dataarray) {
                    MQOrderListmodel *model = [MQOrderListmodel mj_objectWithKeyValues:dic];
                    [_waitmakesuredatasource addObject:model];
                }
                _waitconfirmpage ++;
            }
                break;
            case 4:
            {
                [_flishdatasource removeAllObjects];
                
                for (NSDictionary *dic in dataarray) {
                    MQOrderListmodel *model = [MQOrderListmodel mj_objectWithKeyValues:dic];
                    [_flishdatasource addObject:model];
                }
                _confimpage ++;
            }
                break;
                
            default:
                break;
        }
        
        [_ordertableview reloadData];
        [_ordertableview.mj_header endRefreshing];
        
    } failure:^(NSError *error) {
       
        [_ordertableview.mj_header endRefreshing];
    }];

    
    
}
-(void)getmoredata{
    
    MQBaseUserInfoModel *userinfo = [MQBaseUserInfoModel shareInstant];
    [userinfo loadUserinfoFromSadeBox];
    
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    
    NSArray *paramarray = @[@"all",@"wait_pay",@"wait_ship",@"wait_confirm",@"confim"];
    [param setValue:userinfo.userId forKey:@"userId"];
    if (_currentselectindex !=0) {
        [param setValue:paramarray[_currentselectindex] forKey:@"status"];
    }

    switch (_currentselectindex) {
        case 0:
            [param setValue:[NSString stringWithFormat:@"%ld",_allpage] forKey:@"page"];
            break;
            
        case 1:
              [param setValue:[NSString stringWithFormat:@"%ld",_waitpaypage] forKey:@"page"];
            break;
        case 2:
              [param setValue:[NSString stringWithFormat:@"%ld",_waitphippage] forKey:@"page"];
            break;
        case 3:
              [param setValue:[NSString stringWithFormat:@"%ld",_waitconfirmpage] forKey:@"page"];
            break;
        case 4:
              [param setValue:[NSString stringWithFormat:@"%ld",_confimpage] forKey:@"page"];
            break;
        default:
            break;
    }
    [param setValue:@"10" forKey:@"limit"];
    
    [MQRequestTools GETWithMethod:orderInfo_list params:param success:^(id json) {
        
        NSArray *dataarray = [json valueForKey:@"data"];
        switch (_currentselectindex) {
            case 0:
            {
               
                for (NSDictionary *dic in dataarray) {
                    MQOrderListmodel *model = [MQOrderListmodel mj_objectWithKeyValues:dic];
                    [_dataSource addObject:model];
                }
                _allpage ++;
                
            }
                break;
            case 1:
            {
//                [_waitpayarray removeAllObjects];
                
                for (NSDictionary *dic in dataarray) {
                    MQOrderListmodel *model = [MQOrderListmodel mj_objectWithKeyValues:dic];
                    [_waitpayarray addObject:model];
                }
                _waitpaypage ++;
                
            }
                break;
            case 2:
            {
//                [_waitfahuodatasource removeAllObjects];
                
                for (NSDictionary *dic in dataarray) {
                    MQOrderListmodel *model = [MQOrderListmodel mj_objectWithKeyValues:dic];
                    [_waitfahuodatasource addObject:model];
                }
                _waitphippage++;
                
            }
                break;
            case 3:
            {
//                [_waitmakesuredatasource removeAllObjects];
                
                for (NSDictionary *dic in dataarray) {
                    MQOrderListmodel *model = [MQOrderListmodel mj_objectWithKeyValues:dic];
                    [_waitmakesuredatasource addObject:model];
                }
                _waitconfirmpage++;
            }
                break;
            case 4:
            {
//                [_flishdatasource removeAllObjects];
                
                for (NSDictionary *dic in dataarray) {
                    MQOrderListmodel *model = [MQOrderListmodel mj_objectWithKeyValues:dic];
                    [_flishdatasource addObject:model];
                }
                _confimpage ++;
            }
                break;
                
            default:
                break;
        }
        
        [_ordertableview.mj_footer endRefreshing];
        [_ordertableview reloadData];
        
    } failure:^(NSError *error) {
        [_ordertableview.mj_footer endRefreshing];
    }];
}


@end
